package internal

import (
	"github.com/name5566/leaf/gate"
)

type Player struct {
	Id       int32
	Username string
	Password string
	Agent gate.Agent
}

var playerId int32 = 0
var playerMap = make(map[string]*Player)

func GetPlayerId() int32 {
	playerId = playerId + 1
	return playerId
}

func GetPlayer(username string) *Player {
	player, ok := playerMap[username]
	if ok {
		return player
	} else {
		return nil
	}
}

func GetPlayerByAgent(agent gate.Agent) *Player{
	for username := range playerMap {
		if playerMap[username].Agent == agent {
			return playerMap[username]
		}
	}
	return nil
}

func AddPlayer(username string, passworld string, agent gate.Agent) *Player {

	player := new(Player)
	player.Id = GetPlayerId()
	player.Username = username
	player.Password = passworld
	player.Agent = agent

	playerMap[username] = player

	return player

}

func Broadcast(msg interface{}){
	for username := range playerMap {
		player := playerMap[username]
		player.Agent.WriteMsg(msg)
	}

}

// type Agent struct {
// 	conn *network.TCPConn
// }

// func newAgent(conn *network.TCPConn) network.Agent {
// 	a := new(Agent)
// 	a.conn = conn
// 	return a
// }

// func (a *Agent) Run() {}

// func (a *Agent) OnClose() {}
